package com.signway.javaui.component;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

public class Custom extends Component implements Component.EstimateSizeListener, Component.DrawTask {

    private int width = 200;
    private int height = 200;
    private Paint paint = new Paint();

    public Custom(Context context) {
        super(context);
    }

    public Custom(Context context, AttrSet attrSet) {
        super(context, attrSet);
        setEstimateSizeListener(this);
        addDrawTask(this);
    }

    @Override
    public boolean onEstimateSize(int w, int h) {
        int wMode = EstimateSpec.getMode(w);
        int hMode = EstimateSpec.getMode(h);

        int wConfig = 0;
        switch (wMode) {
            case EstimateSpec.UNCONSTRAINT:
            case EstimateSpec.PRECISE:
                width = EstimateSpec.getSize(w);
            case EstimateSpec.NOT_EXCEED:
                wConfig = EstimateSpec.getSizeWithMode(width, EstimateSpec.PRECISE);
                break;
        }

        int hConfig = 0;
        switch (wMode) {
            case EstimateSpec.UNCONSTRAINT:
            case EstimateSpec.PRECISE:
                height = EstimateSpec.getSize(h);
            case EstimateSpec.NOT_EXCEED:
                hConfig = EstimateSpec.getSizeWithMode(height, EstimateSpec.PRECISE);
                break;
        }

        setEstimatedSize(wConfig, hConfig);
        return true;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        paint.setColor(new Color(0xFF00FFFF));
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.STROKE_STYLE);

        int x = width / 2;
        int y = height / 2;
        canvas.drawCircle(x, y, Math.min(width, height) / 2 - 10, paint);
    }
}
